package com.example.myapplication.utils;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;

import com.example.myapplication.bean.AudioBean;
import com.example.myapplication.databinding.DialogAudioinfoBinding;

import java.text.DecimalFormat;

public class AudioInfoDialog extends Dialog {
    private DialogAudioinfoBinding binding;

    public AudioInfoDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = DialogAudioinfoBinding.inflate(getLayoutInflater());
        CardView root = binding.getRoot();
        setContentView(root);
        //点击关闭按钮，关闭界面。
        binding.tvDetailClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancel();
            }
        });
    }

    public void setDialogWidth(){
        Window window = getWindow();//得到窗口对象
        //窗口信息参数
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        //获取屏幕的宽度
        Display display = window.getWindowManager().getDefaultDisplay();
        layoutParams.width = display.getWidth()-25;
        layoutParams.gravity = Gravity.BOTTOM;
        //设置窗口背景透明
        window.setBackgroundDrawableResource(android.R.color.transparent);
        //设置窗口参数
        window.setAttributes(layoutParams);
    }

    public void setInfo(AudioBean audioBean) {
        binding.tvDetailTime.setText(audioBean.getTime());
        binding.tvDetailName.setText(audioBean.getTitle());
        binding.tvDetailPath.setText(audioBean.getPath());
        String size = calFileSize(audioBean.getFileLength());
        binding.tvDetailSize.setText(size);

    }

    //文件大小换算
    private String calFileSize(long fileLength) {
        DecimalFormat format = new DecimalFormat("#.00");
        if (fileLength >= 1024 * 1024) {
            return format.format(fileLength * 1.0 / (1048576)) + "MB";
        } else if (fileLength >= 1024) {
            return format.format(fileLength*1.0/1024)+"KB";
        } else {
            return fileLength + "B";
        }
    }
}
